using System.Collections.Concurrent;
using System.Net;

namespace SocketPorxyServer;



public sealed class ProxyPool
{
    private readonly ConcurrentQueue<ushort> _queue = new();
    private static readonly List<ushort> Ports = new();

    static ProxyPool()
    {
        for (ushort port = 1024; port < 65534; port++)
        {
            Ports.Add(port);
        }
    }
    
    public void EnqueuePort(ushort port)
    {
        _queue.Enqueue(port);
    }
    
    public bool TryGetPort(out ushort port)
    {
        return _queue.TryDequeue(out port);
    }

    public static ProxyPool NewRandom(IPAddress address, string username, string password)
    {
        var pool = new ProxyPool
        {
            Address = address,
            Username = username,
            Password = password,
        };
        
        using var enumerator = Ports.OrderBy(p=> Guid.NewGuid()).GetEnumerator();
        
        while (enumerator.MoveNext())
            pool.EnqueuePort(enumerator.Current);

        return pool;
    }
    
    public int Count => _queue.Count;
    
    public required IPAddress Address { get; set; }
    
    public required string Username { get; set; }

    public required string Password { get; set; }
}